package scut.carson_ho.searchview;

import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class SampleItemProvider extends BaseItemProvider {

  private List<String> items;
  private Context context;

  SampleItemProvider(List<String> items, Context context) {
    this.items = items;
    this.context = context;
  }

  @Override
  public int getCount() {
    return items == null ? 0 : items.size();
  }

  @Override
  public Object getItem(int i) {
    if (items != null && i >= 0 && i < items.size()) {
      return items.get(i);
    }
    return null;
  }

  @Override
  public long getItemId(int i) {
    return i;
  }

  @Override
  public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
    final Component cpt;
    if (component == null) {
      cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_sample_layout, null, false);
    } else {
      cpt = component;
    }
    String item = items.get(i);
    Text text = (Text) cpt.findComponentById(ResourceTable.Id_item_index);
    text.setText(item);
    return cpt;
  }
}
